package com.lz.sports.vm.score

import com.lz.helper.idcard.IDCardInfo
import com.lz.library.base.BaseModel
import com.lz.library.base.BaseViewModel
import com.lz.sports.bean.ExamResult
import com.lz.sports.bean.ExamSure
import com.lz.sports.core.VerifyManager
import com.lz.sports.db.ExamineeDatabase
import com.lz.sports.ui.checkmodel.FeatureResult
import com.lz.sports.utils.UploadUtils
import com.lz.sports.utils.saveBitmapFile
import com.lz.sports.utils.saveNv21
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

//成绩界面
class ScoreViewModel : BaseViewModel<BaseModel>() {

    //成绩确认方式
    val sureModel = VerifyManager.sureModel().type

    //电子签名成绩确认
    fun sureScore(examResults: List<ExamResult>, idCardInfo: IDCardInfo, path: String, time: Long) {
        launch {
            withContext(Dispatchers.Default) {
                val sureList = ArrayList<ExamSure>()
                val signatureFile = File(UploadUtils.examSurePath(), "signature_${System.currentTimeMillis()}.jpg")
                File(path).takeIf { it.exists() && it.isFile }?.renameTo(signatureFile)
                val cardFile = File(UploadUtils.examSurePath(), "card_${System.currentTimeMillis()}.jpg")
                idCardInfo.photoBitmap()?.also { saveBitmapFile(it, cardFile.absolutePath, true) }

                examResults.forEach {
                    sureList.add(ExamSure(it, sureModel, time).apply {
                        signatureImage = signatureFile.name
                        cardImage = cardFile.name
                    })
                }
                ExamineeDatabase.get().insertExamSure(sureList)
            }
        }
    }

    //确认成绩, 指纹
    fun sureScore(examResults: List<ExamResult>, idCardInfo: IDCardInfo, threshold: Int, time: Long) {
        launch {
            withContext(Dispatchers.Default) {
                val sureList = ArrayList<ExamSure>()
                val cardFile = File(UploadUtils.examSurePath(), "card_${System.currentTimeMillis()}.jpg")
                idCardInfo.photoBitmap()?.also { saveBitmapFile(it, cardFile.absolutePath, true) }

                examResults.forEach {
                    sureList.add(ExamSure(it, sureModel, time).apply {
                        cardImage = cardFile.name
                        verificationType = 1
                        fingerMate = threshold.toFloat()
                    })
                }
                ExamineeDatabase.get().insertExamSure(sureList)
            }
        }
    }

    //确认成绩, 人脸识别
    fun sureScore(examResults: List<ExamResult>, result: FeatureResult, time: Long) {
        launch {
            withContext(Dispatchers.Default) {
                val sureList = ArrayList<ExamSure>()

                val sceneFile = File(UploadUtils.examSurePath(), "scene_${System.currentTimeMillis()}.jpg")
                saveNv21(result.nvData, result.w, result.h, sceneFile)

                var cardImage: String? = null
                var idCardPhotoMate = 0f
                if (result.photoParams?.isSuccess() == true) {
                    val cardFile = File(UploadUtils.examSurePath(), "card_${System.currentTimeMillis()}.jpg")
                    saveBitmapFile(result.photoParams.bitmap, cardFile, false)
                    cardImage = cardFile.name
                    idCardPhotoMate = result.photoParams.thresholdResult
                }

                var photoImage: String? = null
                var examPhotoMate = 0f
                if (result.examParams?.isSuccess() == true) {
                    val examFile = File(UploadUtils.examSurePath(), "exam_${System.currentTimeMillis()}.jpg")
                    saveBitmapFile(result.examParams.bitmap, examFile, false)
                    photoImage = examFile.name
                    examPhotoMate = result.examParams.thresholdResult
                }

                examResults.forEach {
                    val examSure = ExamSure(it, sureModel, time)
                    examSure.verificationType = 0
                    examSure.sceneImage = sceneFile.name

                    if (cardImage != null) {
                        examSure.cardImage = cardImage
                        examSure.idCardPhotoMate = idCardPhotoMate
                    }

                    if (photoImage != null) {
                        examSure.photoImage = photoImage
                        examSure.examPhotoMate = examPhotoMate
                    }
                    sureList.add(examSure)
                }
                ExamineeDatabase.get().insertExamSure(sureList)
            }
        }
    }

}